package cn.always.xiajia.admin.sys.service.impl;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Service;

import cn.always.xiajia.admin.sys.entity.SysRoleMenu;
import cn.always.xiajia.admin.sys.mapper.SysRoleMenuMapper;
import cn.always.xiajia.admin.sys.service.SysRoleMenuService;
import cn.always.xiajia.framework.common.util.ExtCollUtil;
import cn.always.xiajia.framework.common.util.ExtCollectionUtils;
import cn.always.xiajia.framework.mybatis.core.service.impl.BaseServiceImplX;

/**
 * 
 * @author xgj
 *
 */
@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl extends BaseServiceImplX<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

	/**
	 * 根据菜单获取对应角色Id
	 * 
	 * @param menuId
	 * @return
	 */
	@Override
	public Set<Long> getRoleIdByMenuId(Long menuId) {
		if (menuId == null) {
			return Collections.emptySet();
		}
		Map<String, Object> param = new HashMap<>();
		param.put("EQ_menuId", menuId);
		return ExtCollectionUtils.convertSet(xjList(param), SysRoleMenu::getRoleId);
	}

	/**
	 * 根据角色获取对应菜单Id
	 * 
	 * @param menuId
	 * @return
	 */
	@Override
	public Set<Long> getMenuIdByRoleId(Long roleId) {
		return getMenuIdByRoleIds(Collections.singletonList(roleId));
	}

	/**
	 * 根据角色们获取对应菜单Id
	 * 
	 * @param menuId
	 * @return
	 */
	@Override
	public Set<Long> getMenuIdByRoleIds(Collection<Long> roleIds) {
		if (ExtCollUtil.isEmpty(roleIds)) {
			return Collections.emptySet();
		}
		Map<String, Object> param = new HashMap<>();
		param.put("IN_roleId", roleIds);
		return ExtCollectionUtils.convertSet(xjList(param), SysRoleMenu::getMenuId);
	}
}
